package cn.iocoder.yudao.module.shop.controller.admin.handlestrategy;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.handlestrategy.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.handlestrategy.ShopHandleStrategyDO;
import cn.iocoder.yudao.module.shop.convert.handlestrategy.ShopHandleStrategyConvert;
import cn.iocoder.yudao.module.shop.service.handlestrategy.ShopHandleStrategyService;

@Tag(name = "管理后台 - 手工费结算策略")
@RestController
@RequestMapping("/shop/handle-strategy")
@Validated
public class ShopHandleStrategyController {

    @Resource
    private ShopHandleStrategyService handleStrategyService;

    @PostMapping("/create")
    @Operation(summary = "创建手工费结算策略")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy:create')")
    public CommonResult<Long> createHandleStrategy(@Valid @RequestBody ShopHandleStrategyCreateReqVO createReqVO) {
        return success(handleStrategyService.createHandleStrategy(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新手工费结算策略")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy:update')")
    public CommonResult<Boolean> updateHandleStrategy(@Valid @RequestBody ShopHandleStrategyUpdateReqVO updateReqVO) {
        handleStrategyService.updateHandleStrategy(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除手工费结算策略")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy:delete')")
    public CommonResult<Boolean> deleteHandleStrategy(@RequestParam("id") Long id) {
        handleStrategyService.deleteHandleStrategy(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得手工费结算策略")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy:query')")
    public CommonResult<ShopHandleStrategyRespVO> getHandleStrategy(@RequestParam("id") Long id) {
        ShopHandleStrategyDO handleStrategy = handleStrategyService.getHandleStrategy(id);
        return success(ShopHandleStrategyConvert.INSTANCE.convert(handleStrategy));
    }

    @GetMapping("/getvalid")
    @Operation(summary = "获得手工费结算策略")
    @Parameter(name = "tenantId", description = "店铺id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy:query')")
    public CommonResult<ShopHandleStrategyRespVO> getValidHandleStrategy(@RequestParam("tenantId") Long tenantId) {
        ShopHandleStrategyDO handleStrategy = handleStrategyService.getValidStrategy(tenantId);
        return success(ShopHandleStrategyConvert.INSTANCE.convert(handleStrategy));
    }

    @GetMapping("/list")
    @Operation(summary = "获得手工费结算策略列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy:query')")
    public CommonResult<List<ShopHandleStrategyRespVO>> getHandleStrategyList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopHandleStrategyDO> list = handleStrategyService.getHandleStrategyList(ids);
        return success(ShopHandleStrategyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得手工费结算策略分页")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy:query')")
    public CommonResult<PageResult<ShopHandleStrategyRespVO>> getHandleStrategyPage(@Valid ShopHandleStrategyPageReqVO pageVO) {
        PageResult<ShopHandleStrategyDO> pageResult = handleStrategyService.getHandleStrategyPage(pageVO);
        return success(ShopHandleStrategyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出手工费结算策略 Excel")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy:export')")
    @OperateLog(type = EXPORT)
    public void exportHandleStrategyExcel(@Valid ShopHandleStrategyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopHandleStrategyDO> list = handleStrategyService.getHandleStrategyList(exportReqVO);
        // 导出 Excel
        List<ShopHandleStrategyExcelVO> datas = ShopHandleStrategyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "手工费结算策略.xls", "数据", ShopHandleStrategyExcelVO.class, datas);
    }

}
